const app = getApp()
Page({
  data: {
    id: 0,
    userName: '',
    telNumber: '',
    room: '',
    buildingName: '',
    schoolName: '',
    buildingId: '',
    schoolId: '',
    detailInfo: '',
    is_default: 0,
    configState: false,
    configContent: '',
    dialogBodyStyle: {
      width: '70%',
      height: '250rpx'
    }
  },
  onLoad: function (options) {
    console.log(options)
    if (JSON.stringify(options) !== '{}') {
      this.setData({
        id: options.id,
        userName: decodeURIComponent(options.userName),
        telNumber: options.telNumber,
        room: decodeURIComponent(options.room),
        buildingName: decodeURIComponent(options.buildingName),
        schoolName: decodeURIComponent(options.schoolName),
        buildingId: options.buildingId,
        schoolId: options.schoolId,
        detailInfo: decodeURIComponent(options.detailInfo),
        is_default: options.isDefault,
      })
    }

  },
  onShow: function () {

  },
  changeData(data) {//返回更新值
    if (JSON.stringify(data) !== "{}") {
      this.setData({
        buildingId: data.buildingId,
        schoolId: data.schoolId,
        buildingName: decodeURIComponent(data.buildingName),
        schoolName: decodeURIComponent(data.schoolName)
      })
    }
  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  close() {
    this.setData({
      configState: false
    })
  },
  formSubmit: function (e) {
    let _this = this
    let subObj = e.detail.value
    if (!subObj.userName) {
      this.setData({
        configState: true,
        configContent: '联系人不能为空'
      })
      return
    }
    if (!subObj.telNumber) {
      this.setData({
        configState: true,
        configContent: '手机号不能为空'
      })
      return
    }else{
      if (!app.globalData.phoneCheck.test(subObj.telNumber)) {
        this.setData({
          configState: true,
          configContent: '请输入正确的手机号！'
        })
        return
      }
    }
    if (!subObj.buildingId) {
      this.setData({
        configState: true,
        configContent: '学校楼栋不能为空'
      })
      return
    }
    if (!subObj.room) {
      this.setData({
        configState: true,
        configContent: '寝室号码不能为空'
      })
      return
    }
    console.log(subObj)

    app.Http.http({
      url: app.Api.AddressSave,
      method: 'POST',
      params: {
        id: Number(this.data.id),
        userName: subObj.userName,
        telNumber: subObj.telNumber,
        room: subObj.room,
        detailInfo: subObj.detailInfo,
        schoolId: Number(subObj.schoolId),
        buildingId: Number(subObj.buildingId),
        is_default: Number(subObj.is_default)
      }
    }).then(data => {
      console.log(data)
      wx.showToast({
        title: '你的地址信息提交成功！',
        icon: 'loading',
        duration: 100,
        mask: !0,
      })
      setTimeout(() => {
        app.goBack({}, 2, 1)
        wx.hideToast()
      }, 100)

    }).catch(err => {
      console.log(err)
    })
  },
  nameInput(e) {
    this.setData({
      name: e.detail.value
    })
  },
  mobileInput(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  remarksInput(e) {
    this.setData({
      remarks: e.detail.value
    })
  },
  selectAddress() {//选择地址
    app.WxService.navigateTo(`/pages/city-list/index`, { type: 'address' })
  },
  addressDefault(e) {//设置默认
    this.setData({
      is_default: this.data.is_default == 1 ? 0 : 1
    })
  }

})